package com.learn.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.shop.pojo.entriy.SNovelCrawlingHistory;
import com.learn.shop.mapper.SNovelCrawlingHistoryMapper;
import com.learn.shop.pojo.vo.PageVo;
import com.learn.shop.service.SNovelCrawlingHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 小说爬取历史(SNovelCrawlingHistory)表服务实现类
 *
 * @author zhengdechang
 * @since 2021-12-30 11:43:34
 */
@Service
public class SNovelCrawlingHistoryServiceImpl implements SNovelCrawlingHistoryService {
    @Autowired
    private SNovelCrawlingHistoryMapper sNovelCrawlingHistoryMapper;


    @Override
    public PageInfo<SNovelCrawlingHistory> list(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        List<SNovelCrawlingHistory> list = sNovelCrawlingHistoryMapper.list();
        return new PageInfo<>(list);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SNovelCrawlingHistory queryById(Integer id) {
        return this.sNovelCrawlingHistoryMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sNovelCrawlingHistory 筛选条件
     * @param pageRequest           分页对象
     * @return 查询结果
     */
    @Override
    public Page<SNovelCrawlingHistory> queryByPage(SNovelCrawlingHistory sNovelCrawlingHistory, PageRequest pageRequest) {
        long total = this.sNovelCrawlingHistoryMapper.count(sNovelCrawlingHistory);
        return new PageImpl<>(this.sNovelCrawlingHistoryMapper.queryAllByLimit(sNovelCrawlingHistory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sNovelCrawlingHistory 实例对象
     * @return 实例对象
     */
    @Override
    public SNovelCrawlingHistory insert(SNovelCrawlingHistory sNovelCrawlingHistory) {
        this.sNovelCrawlingHistoryMapper.insert(sNovelCrawlingHistory);
        return sNovelCrawlingHistory;
    }

    /**
     * 修改数据
     *
     * @param sNovelCrawlingHistory 实例对象
     * @return 实例对象
     */
    @Override
    public SNovelCrawlingHistory update(SNovelCrawlingHistory sNovelCrawlingHistory) {
        this.sNovelCrawlingHistoryMapper.update(sNovelCrawlingHistory);
        return this.queryById(sNovelCrawlingHistory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sNovelCrawlingHistoryMapper.deleteById(id) > 0;
    }
}
